package com.demo.redis.configuration;

import com.alibaba.fastjson.support.spring.FastJsonRedisSerializer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisNode;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.StringRedisSerializer;
import redis.clients.jedis.HostAndPort;
import redis.clients.jedis.JedisCluster;
import redis.clients.jedis.JedisPoolConfig;

import java.util.HashSet;
import java.util.Set;

@Configuration
@Import(RedisPoolProperties.class)
public class ClusterRedisConfig {

    @Bean
    public JedisPoolConfig jedisPoolConfig(RedisPoolProperties redisPoolProperties) {
        JedisPoolConfig jedisPoolConfig = new JedisPoolConfig();
        jedisPoolConfig.setMaxTotal(redisPoolProperties.getMaxTotal());
        jedisPoolConfig.setMaxIdle(redisPoolProperties.getMaxIdle());
        jedisPoolConfig.setMinIdle(redisPoolProperties.getMinIdle());
        jedisPoolConfig.setTestOnBorrow(redisPoolProperties.getTestOnBorrow());
        jedisPoolConfig.setTestOnReturn(redisPoolProperties.getTestOnReturn());
        jedisPoolConfig.setFairness(redisPoolProperties.getFairness());
        jedisPoolConfig.setMaxWaitMillis(redisPoolProperties.getMaxWaitMillis());
        jedisPoolConfig.setBlockWhenExhausted(redisPoolProperties.getBlockWhenExhausted());
        return jedisPoolConfig;
    }

    @Bean
    public JedisCluster jedisCluster(JedisPoolConfig jedisPoolConfig, RedisPoolProperties redisProperties) {
        Set<HostAndPort> jedisClusterNode = new HashSet<>();
        jedisClusterNode.add(new HostAndPort("192.168.115.128", 7001));
        jedisClusterNode.add(new HostAndPort("192.168.115.128", 7002));
        jedisClusterNode.add(new HostAndPort("192.168.115.128", 7003));
        return new JedisCluster(jedisClusterNode, 5000, 10, jedisPoolConfig);
    }

    @Bean
    public RedisTemplate redisTemplate(JedisPoolConfig jedisPoolConfig, RedisPoolProperties redisProperties) {
        RedisClusterConfiguration redisClusterConfiguration = new RedisClusterConfiguration();
        redisClusterConfiguration.addClusterNode(new RedisNode("192.168.115.128", 7001));
        redisClusterConfiguration.addClusterNode(new RedisNode("192.168.115.128", 7002));
        redisClusterConfiguration.addClusterNode(new RedisNode("192.168.115.128", 7003));
        redisClusterConfiguration.setMaxRedirects(500);
        JedisConnectionFactory jedisConnectionFactory = new JedisConnectionFactory(redisClusterConfiguration, jedisPoolConfig);
        jedisConnectionFactory.afterPropertiesSet();
        RedisTemplate<String, Object> redisTemplate = new RedisTemplate<>();
        redisTemplate.setConnectionFactory(jedisConnectionFactory);
        redisTemplate.setKeySerializer(new StringRedisSerializer());
        redisTemplate.setValueSerializer(new FastJsonRedisSerializer<>(Object.class));
        return redisTemplate;
    }
}
